function getFormatTime(timestamp) {
  newDate = new Date(timestamp)
  month = newDate.getMonth() + 1;
  date = newDate.getDate()
  if (month < 10) {
    month = '0' + month
  }
  if (date < 10) {
    date = '0' + date
  }
  formatTime = [
    newDate.getFullYear(),
    month,
    date
  ].join('')  // alert(formatTime)
  return formatTime
}

function rendergoole(urls, names) {
  var myDate = new Date()
  var cTimestamp = (new Date())
  today = getFormatTime(cTimestamp - 2 * 24 * 3600 * 1000);
  aweekago = getFormatTime(cTimestamp - 8 * 24 * 3600 * 1000)
  var i = urls.length;
  //console.log(i);
  if (!i) {
    return phantom.exit();
  }
  
  var url = urls.pop().replace('aweekago', aweekago).replace('today', today);

 // console.log('---> Begin to open the url :' + url);
  var name = names.pop();
  var page = require('webpage').create();
  page.viewportSize = {width: 1600, height: 1600};
  
  console.log('---> Destination google url:', url);
  console.log('---> The file name:', name);
  console.log('---> jenkins time0  :' + jenkinsTime);
  page.open('https://accounts.google.com/ServiceLogin?service=analytics&passive=1209600&continue=https://analytics.google.com/analytics/web/?pli=1#my-reports/4hoGkPWnT0ayqLw3Ebq6YQ/a6473166w119064719p124551733&followup=https://analytics.google.com/analytics/web/?pli=1', function () {
    page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js', function () {
      console.log("---> Begin login google account ");
      page.evaluate(function () {
        $('#Email').val('yu_liu@foxitsoftware.com');
        $('#next').click();
      });
	  
      window.setTimeout(function () {
          page.evaluate(function () {
            $('#Passwd').val('qweasd123456');
            $('#signIn').click();
          });

          console.log("---> Finish login google account ");

          window.setTimeout(function () {
            console.log("---> open url:" + url);


            page.open(url, function (status) {
            console.log("---> open url:" + url);
              window.setTimeout(function () {
			  
			    page.evaluate(function(jenkinsTime) {
                var today = new Date();
				console.log('---> current browser time :' + today)
                var b =window.document.getElementsByTagName('body')[0].children[0]
                var p=window.document.createElement("p");
                p.style.fontSize="30px"
                p.style.color="red"
                console.log('---> jenkins time :' + jenkinsTime);
                p.innerText="JenkinsTime:" + jenkinsTime  + "    Demo2 Current Time:"+ today;
				b.parentNode.insertBefore(p,b);
  }, jenkinsTime);
			  
                page.render(name+'.png');
                page.render(name+'.pdf');
                console.log("---> Finish capture:" + name+'.png And' + name+'.pdf')
                console.log("---> urls.length:" + urls.length)
                console.log("")
                console.log("")

                rendergoole(urls,names );
                //  phantom.exit();
              }, 50000);
            });
          }, 50000);

      }, 50000);
    });
  });
}

var webPage = require('webpage');
var system = require('system');
if (system.args.length === 1) {
    console.log('Please pass the jenkins time and the server time !');
}

jenkinsTime = system.args[1]
 console.log('jenkins time ' + jenkinsTime);
var urls = [
    'https://analytics.google.com/analytics/web/?pli=1#my-reports/4hoGkPWnT0ayqLw3Ebq6YQ/a6473166w119064719p124551733/?_u.date00=aweekago&_u.date01=today/',
    'https://analytics.google.com/analytics/web/#report/conversions-goal-funnel/a6473166w119064719p131223748/?_u.date00=aweekago&_u.date01=today&_.goalOption=1/',
    'https://analytics.google.com/analytics/web/#report/conversions-goal-funnel/a6473166w119064719p131223748/?_u.date00=aweekago&_u.date01=today&_.goalOption=2/',
	'https://analytics.google.com/analytics/web/?authuser=0#report/visitors-overview/a6473166w119064719p131223748/?_u.date00=aweekago&_u.date01=today/',
	'https://analytics.google.com/analytics/web/?authuser=0#report/conversions-goal-funnel/a6473166w119064719p131223748/?_u.date00=aweekago&_u.date01=today&_.goalOption=3/',
	'https://analytics.google.com/analytics/web/?authuser=0#report/conversions-goal-funnel/a6473166w119064719p131223748/?_u.date00=aweekago&_u.date01=today&_.goalOption=4/',
	'https://analytics.google.com/analytics/web/#report/visitors-overview/a6473166w119064719p131223748/?_u.date00=aweekago&_u.date01=today/'
];


var names=["google1" , "google2","google3","google4","google5","google6" , 'google7'];

rendergoole(urls ,names );
